$('document').ready(function () {
    // 加载页面时触发的事件
    $('#zj').hover(function () {
        $(this).css('background-color', 'transparent')
    })
    $('#zj1').hover(function () {
        $(this).css('background-color', 'transparent')
    })
    if (getCookie('code') == 200) {
        
    } else {
        window.location.href="home.html"
    }
    if ($(window).width() > 1000) {
        $('#Question-Types-content li').css('width', '244px')
        $('#item-bank-checkwrap input').css({
            'width': '13px',
        })
    } else if ($(window).width() < 776) {
        $('#Question-Types-content li').css('width', '88%')
        $('#item-bank-checkwrap input').css({
            'width': '13px',
        })
    }
    // 浏览器分辨率变化时触发的事件
    $(window).resize(function () {
        if ($(window).width() > 1000) {
            $('#Question-Types-content li').css('width', '244px')
            $('#item-bank-checkwrap input').css({
                'width': '13px',
            })
        } else if ($(window).width() < 776) {
            $('#Question-Types-content li').css('width', '88%')
            $('#item-bank-checkwrap input').css({
                'width': '13px',
            })
        }
    })
    // 加载课程
    $.ajax({
        type: 'GET',
        url: 'http://111.229.120.67/gen/all-course-list',
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.statusCode == 0) {
                var array = data.allcourseList
                for (var i = 0; i < array.length; i++) {
                    if (array[i].ispublic == true) {
                        var course = $('<a href="javascript:void(0)" value="' + array[i].courseid + '"></a>')
                        for (var k = 0; k < array.length; k++) {
                            var cscontent = array[i].name
                        }
                        course.append(cscontent)
                        $('#Question-Types-course').append(course)
                    }

                }
            } else {
                $('#myModal-public').modal({})
                $('#myModal-public h4').html('请求超时')
                $('#myModal-public-body').html('请求超时，请重试')
            }
        }
    })

    // 所属课程的点击事件
    var submitda = 0 //判断用户是否提交过答案
    var courseid = 0
    $("#Question-Types-course").on('mouseenter', function () {
        $("#Question-Types-course a").off('click').click(function () {
            $('#Question-Types-answer').css({
                'margin-top':0,
                'border':0,
                'background-color':'#edf2ee'
            })
            $('#Question-Types-content').css({
                'margin-top':0,
                'border':0,
                'background-color':'#edf2ee'
            })
            $('#Question-Types-content-topp').html('')
            $('#Question-Types-content').html('')
            $('#Question-Types-answer-topp').html('')
            $('#Question-Types-answer').html('')
            $('#chapter1').html('')
            $('#Question-Types-course a').removeClass('course')
            $('#Question-Types a').removeClass('type')
            $(this).addClass('course')
            courseid = $(this).attr('value')
            clearCookie('item-bank-chapid')
            clearCookie('item-bank-courseid')
            setCookie('item-bank-courseid', courseid)
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/gen/course-chap',
                data: {
                    "cid": courseid,
                    "pageNum": 1,
                    "pageSize": 100
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    // console.log(data)
                    if (data.statusCode == 0) {
                        var array = data.courseList
                        $('#Question-Types-chapter').html('') //清除多次点击产生的chapter
                        for (var i = 0; i < array.length; i++) {
                            var chapter = $('<a href="javascript:void(0)" value="' + array[i].id + '"></a>')
                            for (var k = 0; k < array.length; k++) {
                                var chcontent = array[i].sectionName
                            }
                            chapter.append(chcontent)
                            $('#Question-Types-chapter').append(chapter)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
    })
    // 所属章节的点击事件
    var zjid = 0
    $("#Question-Types-chapter").on('mouseenter', function () {
        $("#Question-Types-chapter a").click(function () {
            $('#Question-Types-answer').css({
                'margin-top':0,
                'border':0,
                'background-color':'#edf2ee'
            })
            $('#Question-Types-content').css({
                'margin-top':0,
                'border':0,
                'background-color':'#edf2ee'
            })
            $('#Question-Types-content-topp').html('')
            $('#Question-Types-content').html('')
            $('#Question-Types-answer-topp').html('')
            $('#Question-Types-answer').html('')
            $('#chapter1').html('')
            clearCookie('item-bank-chapid')
            zjid = $(this).attr('value')
            setCookie('item-bank-chapid', zjid)
            $('#Question-Types-chapter a').removeClass('chapter')
            $('#Question-Types a').removeClass('type')
            $(this).addClass('chapter')
            $('#Question-Types').html(`
                <a href="javascript:void(0)" value="1">单项选择题</a>
                <a href="javascript:void(0)" value="2">多项选择题</a>
                <a href="javascript:void(0)" value="3">判断题</a>
                <a href="javascript:void(0)" value="4">填空题</a>
                <a href="javascript:void(0)" value="5">简答题</a>
            `)
        })
    });
    // 题目类型点击事件
    var radio_checkbox = 0 //判断题目类型
    $("#Question-Types").on('mouseenter', function () {
        $("#Question-Types a").click(function () {
            $('#Question-Types-content').css({
                'background-color': 'white',
                'border': '1px solid gainsboro'
            })
            $('#Question-Types-answer').css({
                'margin-top':'10px',
                'border':'1px solid gainsboro',
                'background-color':'white'
            })
            length=2
            radioarray_index = -1;
            radioarray_index1 = 2;
            $('#Question-Types a').removeClass('type')
            $(this).addClass('type')
            var type_index = parseInt($(this).attr('value'))
            switch (type_index) { //题目的类型
                case 1:
                    radio_checkbox=0
                    $('#Question-Types-content-topp').html('题干信息：')
                    $('#Question-Types-answer-topp').html('参考答案：')
                    $('#Question-Types-content').html(`
                    <textarea id="radio-text"></textarea>
                    `)
                    $('#Question-Types-answer').html(`
                        <input type="radio" name="radio" style="margin-left:10px" id="radioA" value="1">选项A
                        <input type="radio" name="radio" style="margin-left:10px" id="radioB" value="2">选项B
                    `)
                    $('#chapter1').html(
                        `
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="item-bank-checkwrap">
                                <div style="text-align:center">
                                    <a href="javascript:void(0);" id="up-content-radioadd" class="iconfont icon-jia" style="text-decoration:none;color:blue"></a>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="margin-top:0">
                                    选项A：<textarea id="up-content-radio1" style="width:100%;"></textarea>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                    选项B：<textarea id="up-content-radio2" style="width:100%"></textarea>
                                </div>
                            </div>
                            <button id="item-bank-submit">上传题目</button>
                            `
                    )
                    break
                case 2:
                    $('#Question-Types-content-topp').html('题干信息：')
                    $('#Question-Types-answer-topp').html('参考答案：')
                    radio_checkbox = 1
                    $('#Question-Types-content').html(`
                    <textarea id="radio-text"></textarea>
                    `)
                    $('#Question-Types-answer').html(`
                        <input type="checkbox" style="margin-left:10px" id="checkboxA" value="1">选项A
                        <input type="checkbox" style="margin-left:10px" id="checkboxB" value="2">选项B
                    `)
                    $('#chapter1').html(
                        `
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="item-bank-checkwrap">
                                <div style="text-align:center">
                                    <a href="javascript:void(0);" id="up-content-radioadd" class="iconfont icon-jia" style="text-decoration:none;color:blue"></a>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="margin-top:0">
                                    选项A：<textarea id="up-content-radio1" style="width:100%;"></textarea>
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                    选项B：<textarea id="up-content-radio2" style="width:100%"></textarea>
                                </div>
                            </div>
                            <button id="item-bank-submit">上传题目</button>
                            `
                    )
                    break
                    // <textarea id="radio-answer" style="height:100px"></textarea>
                case 3:
                    radio_checkbox = 2
                    $('#Question-Types-content-topp').html('题干信息：')
                    $('#Question-Types-content').html(`<textarea id="radio-text" style="height:180px"></textarea>`)
                    $('#Question-Types-answer-topp').html('参考答案：')
                    $('#Question-Types-answer').html('')
                    $('#Question-Types-answer').css({
                        'margin-top':0,
                        'border':0,
                        'background-color':'#edf2ee'
                    })
                    $('#chapter1').html(
                        `
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="item-bank-checkwrap">
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="margin-top:0">
                                    <input id="judge-ture" type="radio" name="judge" value="t">对
                                </div>
                                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                    <input id="judge-error" type="radio" name="judge" value="f">错
                                </div>
                            </div>
                            <button id="item-bank-submit">上传题目</button>
                        `
                    )
                    break
                case 4:
                    radio_checkbox = 3
                    $('#Question-Types-content-topp').html('题干信息：')
                    $('#Question-Types-answer-topp').html('参考答案：')
                    $('#Question-Types-answer').html(`
                        <textarea id="text-answer" style="height:100px"></textarea>
                    `)
                    $('#Question-Types-content').html(`<textarea id="radio-text" style="height:200px"></textarea>`)
                    $('#chapter1').html(
                        `
                        <span style="color:gray">用英文的","将答案分隔</span><br>
                        <button id="item-bank-submit">上传题目</button>
                        `
                    )
                    break
                case 5:
                    radio_checkbox = 4
                    $('#Question-Types-content-topp').html('题干信息：')
                    $('#Question-Types-answer-topp').html('参考答案：')
                    $('#Question-Types-answer').html(`
                        <textarea id="text-answer" style="height:100px"></textarea>
                    `)
                    $('#Question-Types-content').html(`<textarea id="radio-text" style="height:200px"></textarea>`)
                    $('#chapter1').html(
                        `
                        <button id="item-bank-submit">上传题目</button>
                        `
                    )
                    break
            }
        })
    });
    // 按钮事件
    var radioarray = ['C', 'D', 'E', 'F', 'G']
    var radioarray_index = -1;
    var radioarray_index1 = 2;
    var length=2
    var options_array=[]
    var options_answers_array=[]
    $('#chapter1').on('mouseover', function () {
        // 选择题添加选项
        $('#up-content-radioadd').off('click').click(function () {
            if (radioarray[radioarray_index] == 'G') {
                $('#myModal-public h4').html('已经超出添加上限')
                $('#myModal-public-body').html('超出选项上限，不能再添加！')
                $('#myModal-public .modal-footer').html(
                    `<button type="button" class="btn" data-dismiss="modal">关闭</button>`
                )
                $('#myModal-public').modal({})
            } else {
                radioarray_index++;
                radioarray_index1++;
                if (radio_checkbox == 1) {
                    $('#item-bank-checkwrap').append(`
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="up-content-radiodiv` + radioarray_index1 + `">
                    选项` + radioarray[radioarray_index] + `：<textarea id="up-content-radio` + radioarray_index1 + `" style="width:100%"></textarea>
                    </div>
                `)
                    $('#Question-Types-answer').append(`
                    <input type="checkbox" style="margin-left:10px" id="radio` + radioarray[radioarray_index] + `" value="`+radioarray_index1+`">选项` + radioarray[radioarray_index] + `
                `)
                } else {
                    $('#item-bank-checkwrap').append(`
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="up-content-radiodiv` + radioarray_index1 + `">
                    选项` + radioarray[radioarray_index] + `：<textarea id="up-content-radio` + radioarray_index1 + `" style="width:100%"></textarea>
                    </div>
                `)
                    $('#Question-Types-answer').append(`
                    <input type="radio" name="radio" style="margin-left:10px" id="radio` + radioarray[radioarray_index] + `" value="`+radioarray_index1+`">选项` + radioarray[radioarray_index] + `
                `)
                }
                length++
            }
        })

        $('#item-bank-submit').off('click').click(function () {
            options_array=[]
            options_answers_array=[]
            for (var i = 0; i < length; i++) {
                options_array[i]=$('#up-content-radio'+parseInt(i+1)+'').val()
            }
            for (var k =0; k < $('#Question-Types-answer input:checked').length; k++) {
                options_answers_array[k]=parseInt($('#Question-Types-answer input:checked').eq(k).attr('value')-1)
            }
            if (getCookie('code') == 200) {
                if(radio_checkbox==0||radio_checkbox==1){
                    $.ajax({
                        type: 'POST',
                        url: 'http://111.229.120.67/stu/push-ques',
                        data: JSON.stringify({
                            "chapter_ids":[getCookie('item-bank-chapid')],
                            "type":radio_checkbox+1,
                            "content":$('#radio-text').val(),
                            "options": options_array,
                            "options_answers": options_answers_array
                        }),
                        xhrFields: {
                            withCredentials: true
                        },
                        contentType: 'application/json',
                        success: function (data) {
                            console.log(data)
                            if (data.statusCode == 0) {
                                $('#myModal-public').modal({})
                                $('#myModal-public h4').html('上传成功')
                                $('#myModal-public-body').html('成功上传题目')
                            } else {
                                $('#myModal-public').modal({})
                                $('#myModal-public h4').html('请求超时')
                                $('#myModal-public-body').html('请求超时，请重试')
                            }
                        }
                    })
                }else if(radio_checkbox==2){
                    $.ajax({
                        type: 'POST',
                        url: 'http://111.229.120.67/stu/push-ques',
                        data: JSON.stringify({
                            "chapter_ids":[getCookie('item-bank-chapid')],
                            "type":radio_checkbox+1,
                            "content":$('#radio-text').val(),
                            "text_answer":$('#item-bank-checkwrap input:checked').attr('value')
                        }),
                        xhrFields: {
                            withCredentials: true
                        },
                        contentType: 'application/json',
                        success: function (data) {
                            console.log(data)
                            if (data.statusCode == 0) {
                                $('#myModal-public').modal({})
                                $('#myModal-public h4').html('上传成功')
                                $('#myModal-public-body').html('成功上传题目')
                            } else {
                                $('#myModal-public').modal({})
                                $('#myModal-public h4').html('请求超时')
                                $('#myModal-public-body').html('请求超时，请重试')
                            }
                        }
                    })
                }else{
                    $.ajax({
                        type: 'POST',
                        url: 'http://111.229.120.67/stu/push-ques',
                        data: JSON.stringify({
                            "chapter_ids":[getCookie('item-bank-chapid')],
                            "type":radio_checkbox+1,
                            "content":$('#radio-text').val(),
                            "text_answer":$('#text-answer').val()
                        }),
                        xhrFields: {
                            withCredentials: true
                        },
                        contentType: 'application/json',
                        success: function (data) {
                            console.log(data)
                            if (data.statusCode == 0) {
                                $('#myModal-public').modal({})
                                $('#myModal-public h4').html('上传成功')
                                $('#myModal-public-body').html('成功上传题目')
                            } else {
                                $('#myModal-public').modal({})
                                $('#myModal-public h4').html('请求超时')
                                $('#myModal-public-body').html('请求超时，请重试')
                            }
                        }
                    })
                }
                
            } else {
                $('#myModal-public h4').html('权限不足')
                $('#myModal-public-body').html('您所在的用户组无权使用该功能')
                $('#myModal-public .modal-footer').html('<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>')
                $('#myModal-public').modal({

                })
            }
        })
    })

    // 查看贡献量
    $('#get-contribute-count').off('click').click(function(){
        $.ajax({
            type: 'GET',
            url: 'http://111.229.120.67/stu/get-contribute-count',
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                console.log(data)
                if (data.statusCode == 0) {
                    $('#myModal-public h4').html('查看贡献题目数')
                    $('#myModal-public-body').html('您贡献题目的数量为：'+data.count+'')
                    $('#myModal-public').modal({})
                } else {
                    $('#myModal-public').modal({})
                    $('#myModal-public h4').html('请求超时')
                    $('#myModal-public-body').html('请求超时，请重试')
                }
            }
        })
    })
    // --------------------------
})